package online.chenlu.blog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import online.chenlu.blog.core.Result;
import online.chenlu.blog.core.ResultCode;
import online.chenlu.blog.model.TeamArea;
import online.chenlu.blog.service.TeamAreaService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Describe TeamAreaController
 * @Author chenlu
 * @Version V1.0
 * @Date 2019/03/27.
 */
@Api(value = "TeamAreaController", tags = "TeamArea")
@Slf4j
@RestController
@RequestMapping("/team/area")
public class TeamAreaController {
    @Resource
    private TeamAreaService teamareaService;

    /**
     * 新增TeamArea
     *
     * @param teamarea
     * @return Result
     */
    @PostMapping
    @ApiOperation(value = "新增TeamArea", notes = "TeamArea")
    public Result add(@RequestBody TeamArea teamarea) {
        teamareaService.save(teamarea);
        log.debug("新增TeamArea.");
        return Result.success();
    }

    /**
     * 删除TeamArea
     *
     * @param id
     * @return Result
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除TeamArea", notes = "TeamArea")
    public Result delete(@PathVariable String id) {
        teamareaService.deleteById(id);
        log.debug("删除TeamArea.");
        return Result.success();
    }

    /**
     * 修改TeamArea
     *
     * @param teamarea
     * @return Result
     */
    @PutMapping
    @ApiOperation(value = "修改TeamArea", notes = "TeamArea")
    public Result update(@RequestBody TeamArea teamarea) {
        teamareaService.update(teamarea);
        log.debug("修改TeamArea.");
        return Result.success();
    }

    /**
     * 查询TeamArea
     *
     * @param id
     * @return Result
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询TeamArea", notes = "TeamArea")
    public Result detail(@PathVariable String id) {
        TeamArea teamarea = teamareaService.findById(id);
        log.debug("查询TeamArea.");

        if (id.equals("1")) {
            return Result.success(teamarea);
        } else if (id.equals("2")) {
            return Result.failure(ResultCode.UNAUTHORIZED);
        }
        return Result.failure("cuowu");
    }

    /**
     * 列表TeamArea（支持分页）
     *
     * @param pageNum  页码
     * @param pageSize 条数
     * @param teamarea 查询条件
     * @return Result
     */
    @GetMapping
    @ApiOperation(value = "列表TeamArea（支持分页）", notes = "TeamArea")
    public Result list(@RequestParam(defaultValue = "0") Integer pageNum,
                       @RequestParam(defaultValue = "0") Integer pageSize,
                       TeamArea teamarea) {
        PageHelper.startPage(pageNum, pageSize);
        List<TeamArea> list = teamareaService.list(teamarea);
        PageInfo pageInfo = new PageInfo(list);
        log.debug("列表TeamArea.");
        return Result.success(pageInfo);
    }

}
